package writeToExcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import constants.Constants;

public class WriteRMSEToExcel {
	
	SXSSFWorkbook wb = new SXSSFWorkbook(-1);	
	
	public WriteRMSEToExcel(String dataset, String fold, double normalRMSE, double[] idealRMSE, double[][] RMSE, double[][] posRMSE, double[][] neuRMSE, double[][] negRMSE) throws IOException{
		String[] epsilons = {"0", "05", "1", "15", "2", "25"};

		String excel_dir = Constants.excel_dir;
		
		Sheet sh0 = wb.createSheet("RMSE");
		
		Row row00 = sh0.createRow(0);
		for(int j=1; j<=5; j++){
			Cell cell = row00.createCell(j);
			cell.setCellValue("margin = "+(0.0 + 0.5 * (j-1)));
		}
		Cell cell06 = row00.createCell(6);
		cell06.setCellValue("ideal case");
		Cell cell07 = row00.createCell(7);
		cell07.setCellValue("baseline");
		for(int k=1; k<4; k++){
			for(int j=1; j<=5; j++){
				Cell cell = row00.createCell(j+k*6+2);
				cell.setCellValue("margin = "+(0.0 + 0.5 * (j-1)));
			}
		}
		
		for(int i=1; i<=6; i++ ){
			Row row = sh0.createRow(i);
			Cell cell0 = row.createCell(0);
			cell0.setCellValue("0."+epsilons[i-1]);
			for(int j=1; j<=5; j++){
				Cell cell = row.createCell(j);
				cell.setCellValue(Double.parseDouble(String.format("%.3f",RMSE[i-1][j-1])));
			}			
			
			Cell cell7 = row.createCell(6);
			cell7.setCellValue(Double.parseDouble(String.format("%.3f",idealRMSE[i-1])));
			
			Cell cell8 = row.createCell(7);
			cell8.setCellValue(Double.parseDouble(String.format("%.3f",normalRMSE)));
			
			Cell cell6 = row.createCell(6+2);
			cell6.setCellValue("0."+epsilons[i-1]);
			for(int j=1; j<=5; j++){
				Cell cell = row.createCell(j+6+2);
				cell.setCellValue(Double.parseDouble(String.format("%.3f",posRMSE[i-1][j-1])));
			}			
			Cell cell12 = row.createCell(12+2);
			cell12.setCellValue("0."+epsilons[i-1]);
			for(int j=1; j<=5; j++){
				Cell cell = row.createCell(j+12+2);
				cell.setCellValue(Double.parseDouble(String.format("%.3f",neuRMSE[i-1][j-1])));
			}
			Cell cell18 = row.createCell(18+2);
			cell18.setCellValue("0."+epsilons[i-1]);
			for(int j=1; j<=5; j++){
				Cell cell = row.createCell(j+18+2);
				cell.setCellValue(Double.parseDouble(String.format("%.3f",negRMSE[i-1][j-1])));
			}
		}
		Row row70 = sh0.createRow(7);
		Cell cell00 = row70.createCell(0);
		cell00.setCellValue("baseline");
		for(int j=1; j<=6; j++){
			Cell cell = row70.createCell(j);
			cell.setCellValue(Double.parseDouble(String.format("%.3f",normalRMSE)));
		}	
		for(int k=1; k<=6; k++){
			Row row7 = sh0.createRow(7+k);
			Cell cell0 = row7.createCell(0);
			cell0.setCellValue("ideal epsilon = 0."+epsilons[k-1]);
			for(int j=1; j<=6; j++){
				Cell cell = row7.createCell(j);
				cell.setCellValue(Double.parseDouble(String.format("%.3f",idealRMSE[k-1])));
			}	
		}
		
		FileOutputStream file = new FileOutputStream(new File(excel_dir + dataset + "-fold-" + fold + "-RMSE.xlsx"));
		wb.write(file);
	}

}
